﻿using System.Collections.Generic;
using System.Linq;
using BlankMvc.Dao.Attributes;
using BlankMvc.Dao.Entities.Base;
using NHibernate;
//using System.Linq;
using NHibernate.Context;
using NHibernate.Linq;

namespace BlankMvc.Dao.Repository.Base {
	/// <summary>
	/// Base class for all repositories those uses NHibernate.
	/// </summary>
	/// <typeparam name="TEntity">Entity type</typeparam>
	/// <typeparam name="TPrimaryKey">Primary key type of the entity</typeparam>
	public abstract class RepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey> where TEntity : Entity<TPrimaryKey> {
		/// <summary>
		/// Gets the NHibernate session object to perform database operations.
		/// </summary>
		protected ISession Session { get { return UnitOfWork.Current.Session; } }

		/// <summary>
		/// Used to get a IQueryable that is used to retrive object from entire table.
		/// </summary>
		/// <returns>IQueryable to be used to select entities from database</returns>
		protected IQueryable<TEntity> Query {
			get {
				return Session.Query<TEntity>();
			}
		}

		/// <summary>
		/// Used to get a IQueryable that is used to retrive object from entire table.
		/// </summary>
		/// <returns>IQueryable to be used to select entities from database</returns>
		public List<TEntity> All() {
			return Session.Query<TEntity>().ToList();
		}

		/// <summary>
		/// Gets an entity.
		/// </summary>
		/// <param name="key">Primary key of the entity to get</param>
		/// <returns>Entity</returns>
		public TEntity Get(TPrimaryKey key) {
			return Session.Get<TEntity>(key);
		}

		/// <summary>
		/// Inserts a new entity.
		/// </summary>
		/// <param name="entity">Entity</param>
		public void Insert(TEntity entity) {
			Session.Save(entity);
		}

		/// <summary>
		/// Updates an existing entity.
		/// </summary>
		/// <param name="entity">Entity</param>
		public void Update(TEntity entity) {
			Session.Update(entity);
		}

		/// <summary>
		/// Deletes an entity.
		/// </summary>
		/// <param name="id">Id of the entity</param>
		public void Delete(TPrimaryKey id) {
			Session.Delete(Session.Load<TEntity>(id));
		}
	}
}
